package com.travelsmart.client.apresentacao.widget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.UrlBuilder;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.travelsmart.client.apresentacao.i18n.I18nMessages;
import com.travelsmart.client.apresentacao.image.ImagesResource;
import com.travelsmart.client.apresentacao.model.SmartHotelModel;
import com.travelsmart.client.apresentacao.screen.BaseScreen;
import com.travelsmart.client.control.AdminServiceAsync;

public class FormMenuBar extends MenuBar{
	
	public FormMenuBar(DeckPanel centerPanel){
		super();
		criarMenu(centerPanel);
	}
	private void criarMenu(final DeckPanel centerPanel) {
		
		this.setAutoOpen(false);
		this.setAnimationEnabled(true);
		this.addItem(new SafeHtml() {
			private static final long serialVersionUID = 5238720098423054054L;

			@Override
			public String asString() {
				return "<span id='menu_home'>Home</span>";
			}
		}, new Command() {
			@Override
			public void execute() {
				centerPanel.showWidget(0);
			}
		});
		this.addItem(new SafeHtml(){
			private static final long serialVersionUID = 8730937952719996099L;
			@Override
			public String asString() {
				return "<span id='menu_hotel'>"+I18nMessages.INSTANCE.hotel()+"</span>";
			}
		}, hotelMenu(centerPanel));
		this.addItem(new MenuItem(I18nMessages.INSTANCE.idioma(), localMenu()));
		MenuBar moneyMenu = new MenuBar(true);
		this.addItem(new MenuItem(I18nMessages.INSTANCE.moeda(), moneyMenu));
		this.addItem(new SafeHtml() {
			private static final long serialVersionUID = 1700590767522346332L;

			@Override
			public String asString() {
				return "<span id='menu_sair'>" + I18nMessages.INSTANCE.sair() + "</span>";
			}
		}, new Command() {
			@Override
			public void execute() {
				AdminServiceAsync.service.logout(new AsyncCallback<Void>() {
					
					@Override
					public void onSuccess(Void result) {
						Window.Location.reload();
					}
					@Override
					public void onFailure(Throwable caught) {
						SmartHotelModel.instance.getErroHotelModel().setErro(caught);
						
					}
				});
				
				
			}
		});
	}

	private MenuBar hotelMenu(final DeckPanel centerPanel) {
		MenuBar hotelMenu = new MenuBar(true);
		// INICIO - EXERC. 3
		hotelMenu.addItem(new SafeHtml() {
			private static final long serialVersionUID = 7965987712098000932L;
			
			@Override
			public String asString() {
				return "<span id='menu_realizar_reserva'>" + I18nMessages.INSTANCE.realizar_reserva() + "</span>"; 
			}
		}, new Command() {
			
			@Override
			public void execute() {
				// seleciona a view (screen 2 = Realizar Reserva)
				((BaseScreen) centerPanel.getWidget(2)).exec();
				centerPanel.showWidget(2);
			}
		});		
		//FIM - EXERC. 3
		
		/*/# INICIO - EXERC. 1*/
		// adiciona a opcao consultar reserva no menu
		hotelMenu.addItem(new SafeHtml() {
			private static final long serialVersionUID = 7965987712098000932L;

			@Override
			public String asString() {
				return "<span id='menu_consultar_reserva'> "+ I18nMessages.INSTANCE.consultar_reserva() +"</span>";
			}
		}, new Command() {
			
			@Override
			public void execute() {
				((BaseScreen)centerPanel.getWidget(1)).exec();
				centerPanel.showWidget(1);
			}
		});
		
		// FIM - EXERC. 1 #*/
		return hotelMenu;
	}

	private MenuBar localMenu() {
		MenuBar localMenu = new MenuBar(true);
		//*/# INICIO - I18n
		final Image imageBrazil = new Image(ImagesResource.INSTANCE.bandeira_brasil());
		final Image imageEua = new Image(ImagesResource.INSTANCE.bandeira_eua());
		
		MenuItem itemPortugues = new MenuItem(new SafeHtml() {
			
			private static final long serialVersionUID = -2023187638054762688L;

			@Override
			public String asString() {
				return "<img src='"+imageBrazil.getUrl()+"' /> Português";
			}
		}, new Command() {
			@Override
			public void execute() {
				String newLocale = getPathI18n("pt_BR");
				Window.Location.assign(newLocale);
			}
		});
		MenuItem itemIngles = new MenuItem(new SafeHtml() {
			
			private static final long serialVersionUID = -2792208467354534597L;

			@Override
			public String asString() {
				return "<img src='"+imageEua.getUrl()+"' /> English";
			}
		}, new Command() {
			@Override
			public void execute() {
				String newLocale = getPathI18n("en_US");
				Window.Location.assign(newLocale);
			}
		});
		localMenu.addItem(itemPortugues);
		localMenu.addItem(itemIngles);
		// FIM - I18n #*/
		return localMenu;
	}
	
	private String getPathI18n(String locale){
		//*/# INICIO - I18n
		UrlBuilder builder = Window.Location.createUrlBuilder();
		
		builder.removeParameter("locale");
		
		builder.setParameter("locale", locale);
		
		//FIM - I18n #*/
		return builder.buildString();
	}


}
